package com.zpark.entity;

import java.util.*;

/**
 * 评估报告类
 */
public class EvaluateReport {
    private String applicationName;
    private String userIdentify;
    private String loginSequence;
    private long evaluateTime;
    private String cityName;
    private GeoPoint geoPoint;

    public EvaluateReport() {
    }

    //用来存储评估因子
    //key就是评估因子；value是Boolean类型；表示该评估因子是否有风险。true:有风险，false:没有风险
    private Map<RiskFactor,Boolean> metrics=new HashMap<RiskFactor,Boolean>();

    public void signReport(RiskFactor riskFactor,boolean flag){
        metrics.put(riskFactor,flag);
    }

    public EvaluateReport(String applicationName, String userIdentify, String loginSequence, long evaluateTime, String cityName, GeoPoint geoPoint) {
        this.applicationName = applicationName;
        this.userIdentify = userIdentify;
        this.loginSequence = loginSequence;
        this.evaluateTime = evaluateTime;
        this.cityName = cityName;
        this.geoPoint = geoPoint;

        //初始化所有风险因子都是false
        metrics.put(RiskFactor.AREA,false);
        metrics.put(RiskFactor.DEVICE,false);
        metrics.put(RiskFactor.SIMILARITY,false);
        metrics.put(RiskFactor.SPEED,false);
        metrics.put(RiskFactor.TIMESLOT,false);
        metrics.put(RiskFactor.INPUTFEATURE,false);
        metrics.put(RiskFactor.TOTAL,false);
    }

    public String getApplicationName() {
        return applicationName;
    }

    public String getUserIdentify() {
        return userIdentify;
    }

    public String getLoginSequence() {
        return loginSequence;
    }

    public long getEvaluateTime() {
        return evaluateTime;
    }

    public String getCityName() {
        return cityName;
    }

    public GeoPoint getGeoPoint() {
        return geoPoint;
    }

    public Map<RiskFactor, Boolean> getMetrics() {
        return metrics;
    }

    /**
     * 重新toString方法，评估因子按照字母顺序排列，如下所示
     * AREA DEVICE INPUTFEATURE SIMILARITY SPEED TIMESLOT TOTAL
     * true false    false        false    true   false     false
     * @return
     */

    @Override
    public String toString(){
        //先根据风险因子的名字做排序
        //把风险因子对应的值用空格拼接起来
        String report=metrics.keySet()
                .stream()
                .sorted((RiskFactor o1, RiskFactor o2) -> o1.name().compareTo(o2.name()))
                .map(riskFactor -> metrics.get(riskFactor)+"")
                .reduce((v1,v2)->v1+" "+v2)
                .get();
        return applicationName+" "+userIdentify+" "+loginSequence+" "+evaluateTime+" "+cityName+ " "+geoPoint.getLongitude()+","+geoPoint.getLatitude()+" "+report;

    }
}